

***********************************Table H5-H7 without trimming****************************************************


*---------------------------------
*Single-technology decomposition
*--------------------------------

use "D:\Nanjing\2019\decomposition_final\data\single_decomposition",clear


*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
/*
1. MC_fp: firm-product-year level marginal cost
2. c_x_new: product share within a firm
3. theta2_fjt_x: firm-product-year level elasticity of emission
4. tax_emi: implied firm-level emission (vary over year)
5. theta2_E_fjt_firm: firm-year-level elasticity of emission
6. fid=party_id: firm identifer
7. product_code: product identifer
8. status: contiuing firm (status==1), exit firms (status==2), new entrants (status==3)
9: quantity: firm-product-year level quantity


*/
*---------------------------------------------------------
*  Step 1: This part is to construct variables 
*----------------------------------------------------------


*gen tau=1/tax_emi /*the measure of emission policy*/

egen ub_e=pctile(theta2_E_fjt_firm),p(97)
egen lb_e=pctile(theta2_E_fjt_firm),p(3)
*keep if theta2_E_fjt_firm>lb_e|theta2_E_fjt_firm<ub_e
gen indc_y=.
replace indc_y=1 if year==2000
replace indc_y=2 if year==2005

keep if indc_y==1|indc_y==2
*1. generate the number of products each firm produce in each year
gen indc_prod=1
bysort fid indc_y: egen num_prod=sum(indc_prod) /*num_product number of product at firm-year level*/



*****************************************************************
* Define 1. continuing, 2. new entrants and 3. exit firms.
replace year=indc_y
gen party_id=fid

gen dummy1=.
replace dummy1=1 if year==1&(totaloutput!=.&totaloutput!=0)

gen dummy2=.
replace dummy2=1 if year==2&(totaloutput!=.&totaloutput!=0)

sort party_id
by party_id: egen max_d1=max(dummy1)
by party_id: egen max_d2=max(dummy2)

replace dummy1=max_d1 if dummy1==.
replace dummy2=max_d2 if dummy2==.

* (1)status=1: continuing firms
* (2)status=2: exit
* (3)status=3: entrant
gen status=.
replace status=1 if dummy1==1&dummy2==1
replace status=2 if dummy1==1&dummy2==.
replace status=3 if dummy1==.&dummy2==1

bysort party_id: gen n1=_n==1 if status==1
bysort party_id: gen n2=_n==1 if status==2
bysort party_id: gen n3=_n==1 if status==3
*------------------------------------------
*5-1(1): Compute the number of each group of firms
*------------------------------------------
egen N_con=sum(n1) /*number of continuing firms*/
egen N_ex=sum(n2)  /*number of exiters*/
egen N_en=sum(n3)  /*number of new entrants*/
gen N_total=(N_con*2+N_en+N_ex)/2 /*averge number of firms in 2000 and 2005*/
drop n1 n2 n3
**************************************************************************
*---------------------------------------------------
*5-1(2): compute delta_X_j for continuing firms:
*---------------------------------------------------
egen f_product=group(party_id product_code)
sort f_product MC_fp year
duplicates drop f_product year, force
tsset f_product year
tsfill, full
bysort f_product: egen product_code1=max(product_code)
bysort f_product: egen party_id1=max(party_id)
bysort f_product: egen status1=max(status)

bysort f_product: egen N_con1=max(N_con)
bysort f_product: egen N_en1=max(N_en)
bysort f_product: egen N_ex1=max(N_ex)
bysort f_product: egen N_total1=max(N_total)

replace product_code=product_code1
replace party_id=party_id1
replace status=status1
replace N_con=N_con1
replace N_en=N_en1
replace N_ex=N_ex1
replace N_total=N_total1

drop  product_code1 party_id1 status1 N_con1 N_en1 N_ex1 N_total1



sort f_product year
by f_product: gen quantity_l=l.quantity
*--------------------------------------------------------------------------------------------------------------------------
* for continuing firms, we focus on their continuing products
* for a given continuinf firm i, if a product j is NOT produced in both period, then we do not count this firm product pair.
*---------------------------------------------------------------------------------------------------------------------------
*replace quantity=0 if quantity==.& status==1  /*这里可能需要去掉！！！！！*/
*replace quantity_l=0 if quantity_l==. &status==1 &year==2

by f_product: gen diff_quant=quantity-quantity_l if status==1&year==2 /*delta_x_{ij} for continuing firms*/

bysort product_code year: egen diff_quant_s=mean(diff_quant)  if status==1/*delta_x_j for continuing firms*/
egen ub_quant=pctile(diff_quant_s),p(99)
egen lb_quant=pctile(diff_quant_s),p(1)
*replace diff_quant_s=. if diff_quant_s<ub_quant&diff_quant_s>lb_quant

*********************************************************************************************************

*---------------------------------------------------------
*5-1(3): compute delta_mc for continuing firm-product pair
*        compute delta_gamma for continuing firm-product pair
*----------------------------------------------------------
sort f_product year
by f_product: gen MC_fp_lag=l.MC_fp if status==1
by f_product: gen diff_mc=MC_fp-MC_fp_l if status==1&year==2 /*delta_mc_{ij} for continuing firm-product pair*/

by f_product: gen theta2_E_fjt_x_lag=l.theta2_E_fjt_x if status==1
by f_product: gen diff_gamma=theta2_E_fjt_x-theta2_E_fjt_x_lag /*delta_gamma_{ij} for continuing firm-product pair*/




*****************************************************************************************************
*-------------------------------------------------------
*5-1(4): compute mean(mc_{ij}) for continuing firm-product pair
*-------------------------------------------------------
* only generate average mc in the second year for continuing firms.
*replace MC_fp=. if MC_fp<0
gen indc_c=.
replace indc_c=1 if quantity!=.&status==1
bysort party_id product_code: egen mc_indc=sum(indc_c) if status==1
bysort party_id product_code: egen mc_con=mean(MC_fp) if status==1/*mc_{ij}: average margional cost for continuing firms */
replace mc_con=. if status==1&year==1
replace mc_con=. if status==1&mc_indc!=2

******************************************************************************

*-------------------------------------------------------
*5-1(5): compute bar_c{i}
*        compute bar_gamma
*        compute bar_tau_{i}
*        compute bar_emission
*        compute bar_(gamma_c): mean(gamma_i*c_i)
*        compute bar_x_{ij}
*        compute bar_rho: mean share
*        compute bar_gamma_fp: the average elasticity of emission at firm-product level 
*-------------------------------------------------------
bysort party_id: egen bar_c=sum(MC_fp*quantity/2) if status==1


bysort party_id year: egen theta2_E_fjt_firm_1=mean(theta2_E_fjt_firm) 
bysort party_id year: egen tax_emi_1=mean(tax_emi) 
bysort party_id year: egen so2_emission_1=mean(so2_emission) 
replace theta2_E_fjt_firm=theta2_E_fjt_firm_1
replace tax_emi=tax_emi_1
replace so2_emission=so2_emission_1
drop theta2_E_fjt_firm_1 tax_emi_1 so2_emission_1

bysort party_id: egen bar_gamma=mean(theta2_E_fjt_firm) if status==1
bysort party_id: egen bar_tau=mean(1/tax_emi) if status==1 
bysort party_id: egen bar_emission=mean(so2_emission) if status==1
bysort party_id: egen bar_gamma_c=sum(theta2_E_fjt_firm*MC_fp*quantity/2) if status==1


****************************************************************************************
* For each continuing firm, only keep one average value:
bysort party_id: gen n1=_n==1 if status==1
replace bar_gamma=bar_gamma*n1
replace bar_tau=bar_tau*n1
replace bar_emission=bar_emission*n1
replace bar_gamma_c=bar_gamma_c*n1

drop n1
* different than other bar_v, bar_x{ij}, bar_rho{ij} and bar_gamma_fp are at firm-product level.
gen bar_x=(quantity+quantity_l)/2 if status==1 /*for continuing firm-product pair bar_x>0, otherwise bar_x==.*/
capture drop share_new
bysort  party_id year: egen share_new=sum(c_x_new)
gen share_rho=c_x_new/share_new
sort f_product year
by f_product: gen share_rho_lag=l.share_rho
gen bar_rho=(share_rho+share_rho_lag)/2 if status==1 /*for continuing firm-product pair bar_rho>0, otherwise bar_rho==.*/

gen bar_gamma_fp=(theta2_E_fjt_x+theta2_E_fjt_x_lag)/2 if status==1 /*for continuing firm-product pair*/
************************************************************************************************************
*---------------------------------------------------------------------------------
*5-1(6): Group level average: group classification is by firms
*        compute bar_e_con1: average emission for continuing firms in year 2000
*        compute bar_e_con2: average emission for continuing firms in year 2005
*        compute bar_e_en: average emission for entrants in year 2005
*        compute bar_e_ex: average emission for exiters in year 2000
*-------------------------------------------------------------------------------------
bysort party_id year: gen indc_n=_n==1

egen bar_e_con1=sum(so2_emission*indc_n) if year==1&status==1
replace bar_e_con1=bar_e_con1/N_con  /*average emission for all continuing firms in year 2000*/
bysort year: egen indc_n2=max(bar_e_con1)
replace bar_e_con1=indc_n2       /*imput this value into each observation in 2000*/
drop indc_n2



egen bar_e_con2=sum(so2_emission*indc_n) if year==2&status==1
replace bar_e_con2=bar_e_con2/N_con /*average emission for all continuing firms in year 2005*/
bysort year: egen indc_n2=max(bar_e_con2)
replace bar_e_con2=indc_n2       /*input this value into each observation in 2005*/
drop indc_n2



egen bar_e_en=sum(so2_emission*indc_n) if year==2&status==3
replace bar_e_en=bar_e_en/N_en /*average emission for all entrants in year 2005*/
bysort year: egen indc_n2=max(bar_e_en)
replace bar_e_en=indc_n2       /*input this value into each observation in 2005*/
drop indc_n2


egen bar_e_ex=sum(so2_emission*indc_n) if year==1&status==2
replace bar_e_ex=bar_e_ex/N_ex /*average emission for all exiters in year 2000*/
bysort year: egen indc_n2=max(bar_e_ex)
replace bar_e_ex=indc_n2       /*input this value into each observation in 2000*/
drop indc_n2 indc_n


*--------------------------------------------------
*---------------------------------------------------------------------------------
*5-1(6)-2: Group level average: the group classification is by products: 
*        compute n_con: number of continuing products within continuing firms
*        compute n_en: number of new products within continuing firms
*        compute n_ex: number of exit products within continuing firms
*        compute bar_gamma_con1: average elasticity of all continuing firm-product pair in 2000;
*        compute bar_gamma_con2: average elasticity of all continuing firm-product pair in 2005;
*        compute bar_gamma_en: average elasticity of new products within continuing firms in 2005;
*        compute bar_gamma_ex: average elasticity of all exit products in year 2000;
*        compute bar_c_con1: average cost of contiuing firm-product pair in 2000
*        compute bar_c_con2: average cost of contiuing firm-product pair in 2005
*        compute bar_c_en: average cost of new products within continuing firms in 2000
*        compute bar_c_ex: average cost of exit products within continuing firms in 2005
*-------------------------------------------------------------------------------------
gen indc_n1=1 if quantity!=.&quantity_l!=.&status==1 /*indc_n1=1 for continuing firm-product pair*/
gen indc_n2=1 if quantity!=.&quantity_l==.&status==1&year==2 /*indc_n2=1 for new products within continuing firms*/
gen indc_n3=1 if quantity==.&quantity_l!=.&status==1 /*indc_n3=1 for exited products within continuing firms*/
bysort party_id: egen n_con=sum(indc_n1) if status==1 /*number of continuing firm-product pair within each continuing firm i*/
bysort party_id: egen n_en=sum(indc_n2) if status==1 /*number of new products within continuing firms*/
bysort party_id: egen n_ex=sum(indc_n3) if status==1 /*number of exit products withing continuing firms*/



bysort party_id product_code: egen indc_n=max(indc_n1)
replace indc_n1=indc_n
drop indc_n

bysort party_id product_code: egen indc_n=max(indc_n3)
replace indc_n3=indc_n
replace indc_n3=. if year==2


bysort party_id: egen bar_gamma_con1=sum(share_rho*theta2_E_fjt_x*indc_n1) if year==1
replace  bar_gamma_con1= bar_gamma_con1/n_con  /*average elasticity of all continuing firm-product pair in 2000;*/

bysort party_id: egen bar_c_con1=sum(quantity*MC_fp*indc_n1) if year==1
replace  bar_c_con1= bar_c_con1/n_con  /*average costs of all continuing firm-product pair in 2000;*/

*********************************************************************

bysort party_id: egen bar_gamma_con2=sum(share_rho*theta2_E_fjt_x*indc_n1) if year==2
replace  bar_gamma_con2= bar_gamma_con2/n_con  /*average elasticity of all continuing firm-product pair in 2005;*/

bysort party_id: egen bar_c_con2=sum(quantity*MC_fp*indc_n1) if year==2
replace  bar_c_con2= bar_c_con2/n_con  /*average costs of all continuing firm-product pair in 2005;*/
*************************************************************************


bysort party_id: egen bar_gamma_en=sum(share_rho*theta2_E_fjt_x*indc_n2) if year==2
replace  bar_gamma_en= bar_gamma_en/n_en  /*average elasticity of new products introduced by continuing firms in 2005;*/

bysort party_id: egen bar_c_en=sum(quantity*MC_fp*indc_n2) if year==2
replace  bar_c_en= bar_c_en/n_en  /*average costs of new products introduced by continuing firms in 2005;*/

**************************************************************************

bysort party_id: egen bar_gamma_ex=sum(share_rho*theta2_E_fjt_x*indc_n3) if year==1
replace  bar_gamma_ex= bar_gamma_ex/n_ex  /*average elasticity of exited products of continuing firms in 2000;*/
bysort party_id: egen bar_c_ex=sum(quantity*MC_fp*indc_n3) if year==1
replace  bar_c_ex= bar_c_ex/n_ex  /*average costs of new products introduced by continuing firms in 2005;*/

**************************************************************
* fill the value into each cell of continuing firm-product pair
bysort party_id product_code: egen bar_1=max(bar_gamma_con1)
bysort party_id product_code: egen bar_2=max(bar_gamma_con2)
bysort party_id product_code: egen bar_3=max(bar_gamma_en)
bysort party_id product_code: egen bar_4=max(bar_gamma_ex)
bysort party_id product_code: egen bar_5=max(bar_c_con1)
bysort party_id product_code: egen bar_6=max(bar_c_con2)
bysort party_id product_code: egen bar_7=max(bar_c_en)
bysort party_id product_code: egen bar_8=max(bar_c_ex)
replace bar_gamma_con1=bar_1
replace bar_gamma_con2=bar_2
replace bar_gamma_en=bar_3
replace bar_gamma_ex=bar_4
replace bar_c_con1=bar_5
replace bar_c_con2=bar_6
replace bar_c_en=bar_7
replace bar_c_ex=bar_8

drop bar_1 bar_2 bar_3 bar_4 bar_5 bar_6 bar_7 bar_8

drop indc_n indc_n1 indc_n2 indc_n3







/*
*-----------------------
egen bar_gamma_con1=sum(theta2_E_fjt_firm*indc_n) if year==1&status==1
replace bar_gamma_con1=bar_gamma_con1/N_con  /*average elasticity for all continuing firms in year 2000*/
bysort year: egen indc_n2=max(bar_gamma_con1)
replace bar_gamma_con1=indc_n2       /*imput this value into each observation in 2000*/
drop indc_n2


egen bar_gamma_con2=sum(theta2_E_fjt_firm*indc_n) if year==2&status==1
replace bar_gamma_con2=bar_gamma_con1/N_con  /*average elasticity for all continuing firms in year 2000*/
bysort year: egen indc_n2=max(bar_gamma_con2)
replace bar_gamma_con2=indc_n2       /*imput this value into each observation in 2000*/
drop indc_n2


egen bar_gamma_en=sum(theta2_E_fjt_firm*indc_n) if year==2&status==3
replace bar_gamma_en=bar_gamma_en/N_en  /*average elasticity for all entrants in year 2005*/
bysort year: egen indc_n2=max(bar_gamma_en)
replace bar_gamma_en=indc_n2       /*imput this value into each observation in 2005*/
drop indc_n2

egen bar_gamma_ex=sum(theta2_E_fjt_firm*indc_n) if year==1&status==2
replace bar_gamma_ex=bar_gamma_ex/N_ex  /*average elasticity for all exiters in year 2000*/
bysort year: egen indc_n2=max(bar_gamma_ex)
replace bar_gamma_ex=indc_n2       /*imput this value into each observation in 2000*/
drop indc_n2
*/



***************************************************************************************

*-------------------------------------------------------------------------
*5-1(7) compute delta_rho: the change of firm-product share
*-------------------------------------------------------------------------
gen diff_rho=share_rho-share_rho_l if status==1  /*for continuing firm-product pair share change*/

***************************************************************************************

*-------------------------------------------------------------------------
*5-1(8) compute delta_tau: the change of firm-level emission policy
*-------------------------------------------------------------------------


gen tau1=1/tax_emi if year==1& status==1 
bysort party_id: egen tau2=max(tau1) /*for each firm tau2 is the emission policy faced in period 1*/
gen delta_tau=1/tax_emi-tau2 
replace delta_tau=. if status==1&year==1 
bysort party_id: egen delta_tau1=mode(delta_tau)
replace delta_tau=delta_tau1
drop tau1 tau2 delta_tau1

***************************************************************************************

*-------------------------------------------------------------------------
*5-1(9) compute eta_e and ent_x: the number of entered and exit products
*-------------------------------------------------------------------------
gen prod_en=.
gen prod_ex=.
replace prod_en=1 if quantity!=.&quantity_l==.&year==2&status==1
replace prod_ex=1 if quantity==.&(quantity_l!=.)&status==1
bysort party_id: egen eta_e=sum(prod_en)
bysort party_id: egen eta_x=sum(prod_ex)
***************************************************************************************
/*
*We have the following constructed variable at hand:
(1). "bar_emission": average so2 emission for continuing firms in 2000 and 2005;
(2). "N_con", "N_en", "N_ex", and "N_total": number of continuing, entrant, exit and average number of firms.
(3). "bar_tau": the average of 1/tax for each continuing firm in 2000 and 2005;
(4). "bar_gamma": the average of elasticity to emission for each continuing firm in 2000 and 2005;
(5). "bar_c": the average total costs for each firm in 2000 and 2005;
(6). "bar_gamma_c": the avarege gamma*c_i for each firm in 2000 and 2005;
(7). "bar_rho": the average share of continuing firm-product pair in 2000 and 2005
(8). "bar_x": bar_x{ij}, the average quantity of x_ij at firm-product pair during 2000 - 2005;
(9). "bar_gamma_fp" the average elasticity of emission at firm-product level during 2000-2005;
(10). "bar_e_con1", "bar_e_con2": average emission for continuing group in 2000 and 2000, respectively;
(11). "bar_e_en" and "bar_e_ex": average emission for new entrants in year 2005 and for exiters in 2000, respectively.
(12). "bar_gamma_con1", "bar_gamma_con2": average elasticity for continuing firm-product pairs in 2000 and 2000, respectively;
(13). "bar_gamma_en" and "bar_gamma_ex": average elasticity for new  and for exit products within continuing firms, respectively.
(14). "bar_c_con1", "bar_c_con2": average costs for continuing firm-product pairs in 2000 and 2000, respectively;
(15). "bar_c_en" and "bar_c_ex": average costs for new and for exit products within continuing firms, respectively.
(14). "diff_quant": delta_x(ij) the quantity difference for continuing firm-product pair ij.
(16). "diff_quant_s": delta_x(j) the average quantity difference for all continuing firms.
(17). "diff_mc": the difference of firm-product level marginal cost between 2000 and 2005.
(18). "diff_rho": the difference of firm-product level share between 2000 and 2005.
(19). "diff_gamma": the difference of firm-product level emission elasticity between 2000 and 2005;
(20). "eta_e" and "eta_x": the number of entering and exit products for each firm. 
(21). "mc_con": average costs at firm-product level in 2000 and 2005; 
(22). "delta_tau": firm-level emission policy change during 2000-2005: tau_2005-tau_2000;
*/
***************************************************************************************
***************************************************************************************


*--------------------------------------------------------
* Step 2: Construct S_t (scale)
*-------------------------------------------------------- 

gen comp1=bar_emission*(N_en-N_ex) /*No of firms*/

egen comp2=sum(bar_tau*bar_c*bar_gamma*(eta_e-eta_x)) 
replace comp2=N_total/N_con*comp2 /*adjusted number of products*/

gen aa=mc_con*diff_quant_s
egen min_aa=min(aa)
replace aa=. if aa==min_aa
bysort party_id: egen comp3= sum(aa) 
bysort party_id: egen comp31=max(comp3)
egen comp32=sum(bar_tau*bar_gamma*comp3)
replace comp3=N_total/N_con*comp32 /*adjusted average output growth*/
drop comp31 comp32

*Generate S_t
gen St=comp1+comp2+comp3
egen s1=mean(comp1)
egen s2=mean(comp2)
egen s3=mean(comp3)

drop comp1 comp2 comp3
***********************************************************************

*------------------------------------------------------------b
* Step 3: Construct P_t (emission policy)
*------------------------------------------------------------


egen Pt=sum(bar_gamma_c*delta_tau) if status==1
replace Pt=N_total/N_con*Pt   



*****************************************************************************

*------------------------------------------------------------
* Step 4: Construct T_t (Technology)
*------------------------------------------------------------

bysort party_id: egen comp1=sum(bar_rho*diff_gamma)
egen comp12=sum(bar_tau*bar_c*comp1) if status==1
replace comp1=N_total/N_con*comp12 /*EBTC component*/

bysort party_id: egen comp2=sum(bar_x*diff_mc)
egen comp22=sum(bar_tau*bar_gamma*comp2)
replace comp2=N_total/N_con*comp22  /*Eifficiency component*/

gen Tt=comp1+comp2

egen t1=mean(comp1)
egen t2=mean(comp2)
drop comp1 comp12 comp2 comp22
***********************************************************************



*------------------------------------------------------------
* Step 5: Construct R_t (Reallocation)
*------------------------------------------------------------
*gen bar_c_product=(bar_c_con1+bar_c_con2)/2

bysort party_id: egen comp1=sum(mc_con*(diff_quant-diff_quant_s))
egen max_comp1=max(comp1)
egen min_comp1=min(comp1)


replace comp1=comp1/10 if comp1==max_comp1
replace comp1=comp1/10 if comp1==min_comp1
*bysort party_id: egen comp1=sum(bar_c*(diff_quant-diff_quant_s))
egen comp12=sum(bar_tau*bar_gamma*comp1) if status==1
replace comp1=N_total/N_con*comp12 /*Across firms component*/



bysort party_id: egen comp2=sum(bar_gamma_fp*diff_rho) if status==1
egen  comp22=sum(bar_tau*bar_c*comp2) if status==1
replace comp2=N_total/N_con*comp22 /*Across Products component*/

gen Rt=comp1+comp2
egen r1=mean(comp1)
egen r2=mean(comp2)


drop comp1 comp12 comp2 comp22




****************************************************************************************

 
*****************************************************************




*------------------------------------------------------------
* Step 6: Construct C_t (Churning)
*------------------------------------------------------------
gen comp11=N_en*(bar_e_en-bar_e_con2)
egen comp11_new=mode(comp11)
replace comp11=comp11_new
drop comp11_new
gen comp12=N_ex*(bar_e_con1-bar_e_ex)
egen comp12_new=mode(comp12)
replace comp12=comp12_new
drop comp12_new
gen comp1=comp11+comp12 /*Firm-level Churning*/


egen comp2=sum(bar_tau*bar_c*(n_en*(bar_gamma_en-bar_gamma_con2)+n_ex*(bar_gamma_con1-bar_gamma_ex))) if status==1
replace comp2=N_total/N_con*comp2   /*Product-level emissions churning*/


egen comp3=sum(bar_tau*bar_gamma*(n_en*(bar_c_en-bar_c_con2)+n_ex*(bar_c_con1-bar_c_ex))) if status==1
replace comp3=N_total/N_con*comp3 /*Product-level cost churning*/



gen Ct=comp1+comp2+comp3

egen c1=mean(comp1)
egen c2=mean(comp2)
egen c3=mean(comp3)

drop comp1 comp2 comp3










******************************************Table H5: single-tech*****************************************************************

******************************************Table H5: single_tech*****************************************************************


******************************************Table H5: single_tech*****************************************************************



*---------------------------------------------------------
* Row 1 (columns 1-3) of Table H5 (for single-Technology)
*-----------------------------------------------------------
gen Delta_E=St+Pt+Tt+Rt+Ct

gen Scale=St/Delta_E*100
gen Policy=Pt/Delta_E*100
gen Technology=Tt/Delta_E*100
gen Reallocation=Rt/Delta_E*100
gen Churning=Ct/Delta_E*100


rename Reallocation Composition 
replace Composition=Composition+Churning 
replace Technology=Technology+Policy 

sum Scale Composition Technology  Churning



********************Table H6 for single-tech*************************
********************Table H6 for single-tech*************************
*---------------------------------------------------------------
* The following code replicate the subcomponent share in Table 8
*-----------------------------------------------------------------
* (1) Subshare of Scale (row 1 and columns 2-4 of Table 6)
gen share_s1=s1/St*100
gen share_s2=s2/St*100
gen share_s3=s3/St*100

sum share_s1 share_s2 share_s3

**********************************************
* (2)subshare of Composition (row3 and columns 2-4  of Table 6)
gen Rt_new=Rt+Ct
gen share_r1=r1/Rt*100*(Rt/Rt_new)
gen share_r2=r2/Rt*100*(Rt/Rt_new)
gen share_r3=Ct/Rt*100*(Rt/Rt_new)

sum share_r1 share_r2 share_r3
*********************************************
* (3) subshare of Technology (row 5 and columns 2-4 of Table 6)

gen Tt_new=Tt+Pt
gen share_t1=t1/Tt*100*(Tt/Tt_new)
gen share_t2=t2/Tt*100*(Tt/Tt_new)
gen share_t3=Pt/Tt*100*(Tt/Tt_new)

sum share_t1 share_t2 share_t3




********************Table H7 for single-tech*************************
********************Table H7 for single-tech*************************
* (4)subshare of Churning
gen share_c1=c1/Ct*100
gen share_c2=c2/Ct*100
gen share_c3=c3/Ct*100


sum share_c1 share_c2 share_c3

**********************************************************************************************











































*---------------------------------
*Multiple-technology decomposition
*----------------------------------



****************************************************************************************
*decomposition 
use "D:\Nanjing\2019\decomposition_final\data\multiple_decomposition",clear





gen MC_fp=expenditure_m_fjt_x/(theta2_fjt_x*quantity) /*MC_fp: firm-product level marginal cost*/

* 2. emission tax
replace so2_emission=exp(e) /*e contains predicted emission when emission==0*/
bysort year fid: egen ems_new=sum(c_x_new*so2_emission/theta2_E_fjt_x)
gen tax_emi=totaloutput/(Markup_firm*ems_new)  /*firm-year level emission tax*/

* 3. firm-level elasticity of emission
bysort year fid: egen theta2_E_fjt_firm=sum(theta2_E_fjt_x*c_x_new) /*theta2_E_fjt_firm: firm level elasticity of emission*/




*gen tau=1/tax_emi /*the measure of emission policy*/

egen ub_e=pctile(theta2_E_fjt_firm),p(97)
egen lb_e=pctile(theta2_E_fjt_firm),p(3)
*keep if theta2_E_fjt_firm>lb_e|theta2_E_fjt_firm<ub_e
gen indc_y=.
replace indc_y=1 if year==2000
replace indc_y=2 if year==2005

keep if indc_y==1|indc_y==2
*1. generate the number of products each firm produce in each year
gen indc_prod=1
bysort fid indc_y: egen num_prod=sum(indc_prod) /*num_product number of product at firm-year level*/



*****************************************************************
* Define 1. continuing, 2. new entrants and 3. exit firms.
replace year=indc_y
gen party_id=fid

gen dummy1=.
replace dummy1=1 if year==1&(totaloutput!=.&totaloutput!=0)

gen dummy2=.
replace dummy2=1 if year==2&(totaloutput!=.&totaloutput!=0)

sort party_id
by party_id: egen max_d1=max(dummy1)
by party_id: egen max_d2=max(dummy2)

replace dummy1=max_d1 if dummy1==.
replace dummy2=max_d2 if dummy2==.

* (1)status=1: continuing firms
* (2)status=2: exit
* (3)status=3: entrant
gen status=.
replace status=1 if dummy1==1&dummy2==1
replace status=2 if dummy1==1&dummy2==.
replace status=3 if dummy1==.&dummy2==1

bysort party_id: gen n1=_n==1 if status==1
bysort party_id: gen n2=_n==1 if status==2
bysort party_id: gen n3=_n==1 if status==3
*------------------------------------------
*5-1(1): Compute the number of each group of firms
*------------------------------------------
egen N_con=sum(n1) /*number of continuing firms*/
egen N_ex=sum(n2)  /*number of exiters*/
egen N_en=sum(n3)  /*number of new entrants*/
gen N_total=(N_con*2+N_en+N_ex)/2 /*averge number of firms in 2000 and 2005*/
drop n1 n2 n3
**************************************************************************
*---------------------------------------------------
*5-1(2): compute delta_X_j for continuing firms:
*---------------------------------------------------
egen f_product=group(party_id product_code)
sort f_product MC_fp year
duplicates drop f_product year, force
tsset f_product year
tsfill, full
bysort f_product: egen product_code1=max(product_code)
bysort f_product: egen party_id1=max(party_id)
bysort f_product: egen status1=max(status)

bysort f_product: egen N_con1=max(N_con)
bysort f_product: egen N_en1=max(N_en)
bysort f_product: egen N_ex1=max(N_ex)
bysort f_product: egen N_total1=max(N_total)

replace product_code=product_code1
replace party_id=party_id1
replace status=status1
replace N_con=N_con1
replace N_en=N_en1
replace N_ex=N_ex1
replace N_total=N_total1

drop  product_code1 party_id1 status1 N_con1 N_en1 N_ex1 N_total1



sort f_product year
by f_product: gen quantity_l=l.quantity
*--------------------------------------------------------------------------------------------------------------------------
* for continuing firms, we focus on their continuing products
* for a given continuinf firm i, if a product j is NOT produced in both period, then we do not count this firm product pair.
*---------------------------------------------------------------------------------------------------------------------------
*replace quantity=0 if quantity==.& status==1  /*这里可能需要去掉！！！！！*/
*replace quantity_l=0 if quantity_l==. &status==1 &year==2

by f_product: gen diff_quant=quantity-quantity_l if status==1&year==2 /*delta_x_{ij} for continuing firms*/

bysort product_code year: egen diff_quant_s=mean(diff_quant)  if status==1/*delta_x_j for continuing firms*/
egen ub_quant=pctile(diff_quant_s),p(99)
egen lb_quant=pctile(diff_quant_s),p(1)
*replace diff_quant_s=. if diff_quant_s<ub_quant&diff_quant_s>lb_quant

*********************************************************************************************************

*---------------------------------------------------------
*5-1(3): compute delta_mc for continuing firm-product pair
*        compute delta_gamma for continuing firm-product pair
*----------------------------------------------------------
sort f_product year
by f_product: gen MC_fp_lag=l.MC_fp if status==1
by f_product: gen diff_mc=MC_fp-MC_fp_l if status==1&year==2 /*delta_mc_{ij} for continuing firm-product pair*/

by f_product: gen theta2_E_fjt_x_lag=l.theta2_E_fjt_x if status==1
by f_product: gen diff_gamma=theta2_E_fjt_x-theta2_E_fjt_x_lag /*delta_gamma_{ij} for continuing firm-product pair*/




*****************************************************************************************************
*-------------------------------------------------------
*5-1(4): compute mean(mc_{ij}) for continuing firm-product pair
*-------------------------------------------------------
* only generate average mc in the second year for continuing firms.
*replace MC_fp=. if MC_fp<0
gen indc_c=.
replace indc_c=1 if quantity!=.&status==1
bysort party_id product_code: egen mc_indc=sum(indc_c) if status==1
bysort party_id product_code: egen mc_con=mean(MC_fp) if status==1/*mc_{ij}: average margional cost for continuing firms */
replace mc_con=. if status==1&year==1
replace mc_con=. if status==1&mc_indc!=2

******************************************************************************

*-------------------------------------------------------
*5-1(5): compute bar_c{i}
*        compute bar_gamma
*        compute bar_tau_{i}
*        compute bar_emission
*        compute bar_(gamma_c): mean(gamma_i*c_i)
*        compute bar_x_{ij}
*        compute bar_rho: mean share
*        compute bar_gamma_fp: the average elasticity of emission at firm-product level 
*-------------------------------------------------------
bysort party_id: egen bar_c=sum(MC_fp*quantity/2) if status==1


bysort party_id year: egen theta2_E_fjt_firm_1=mean(theta2_E_fjt_firm) 
bysort party_id year: egen tax_emi_1=mean(tax_emi) 
bysort party_id year: egen so2_emission_1=mean(so2_emission) 
replace theta2_E_fjt_firm=theta2_E_fjt_firm_1
replace tax_emi=tax_emi_1
replace so2_emission=so2_emission_1
drop theta2_E_fjt_firm_1 tax_emi_1 so2_emission_1

bysort party_id: egen bar_gamma=mean(theta2_E_fjt_firm) if status==1
bysort party_id: egen bar_tau=mean(1/tax_emi) if status==1 
bysort party_id: egen bar_emission=mean(so2_emission) if status==1
bysort party_id: egen bar_gamma_c=sum(theta2_E_fjt_firm*MC_fp*quantity/2) if status==1


****************************************************************************************
* For each continuing firm, only keep one average value:
bysort party_id: gen n1=_n==1 if status==1
replace bar_gamma=bar_gamma*n1
replace bar_tau=bar_tau*n1
replace bar_emission=bar_emission*n1
replace bar_gamma_c=bar_gamma_c*n1

drop n1
* different than other bar_v, bar_x{ij}, bar_rho{ij} and bar_gamma_fp are at firm-product level.
gen bar_x=(quantity+quantity_l)/2 if status==1 /*for continuing firm-product pair bar_x>0, otherwise bar_x==.*/
capture drop share_new
bysort  party_id year: egen share_new=sum(c_x_new)
gen share_rho=c_x_new/share_new
sort f_product year
by f_product: gen share_rho_lag=l.share_rho
gen bar_rho=(share_rho+share_rho_lag)/2 if status==1 /*for continuing firm-product pair bar_rho>0, otherwise bar_rho==.*/

gen bar_gamma_fp=(theta2_E_fjt_x+theta2_E_fjt_x_lag)/2 if status==1 /*for continuing firm-product pair*/
************************************************************************************************************
*---------------------------------------------------------------------------------
*5-1(6): Group level average: group classification is by firms
*        compute bar_e_con1: average emission for continuing firms in year 2000
*        compute bar_e_con2: average emission for continuing firms in year 2005
*        compute bar_e_en: average emission for entrants in year 2005
*        compute bar_e_ex: average emission for exiters in year 2000
*-------------------------------------------------------------------------------------
bysort party_id year: gen indc_n=_n==1

egen bar_e_con1=sum(so2_emission*indc_n) if year==1&status==1
replace bar_e_con1=bar_e_con1/N_con  /*average emission for all continuing firms in year 2000*/
bysort year: egen indc_n2=max(bar_e_con1)
replace bar_e_con1=indc_n2       /*imput this value into each observation in 2000*/
drop indc_n2



egen bar_e_con2=sum(so2_emission*indc_n) if year==2&status==1
replace bar_e_con2=bar_e_con2/N_con /*average emission for all continuing firms in year 2005*/
bysort year: egen indc_n2=max(bar_e_con2)
replace bar_e_con2=indc_n2       /*input this value into each observation in 2005*/
drop indc_n2



egen bar_e_en=sum(so2_emission*indc_n) if year==2&status==3
replace bar_e_en=bar_e_en/N_en /*average emission for all entrants in year 2005*/
bysort year: egen indc_n2=max(bar_e_en)
replace bar_e_en=indc_n2       /*input this value into each observation in 2005*/
drop indc_n2


egen bar_e_ex=sum(so2_emission*indc_n) if year==1&status==2
replace bar_e_ex=bar_e_ex/N_ex /*average emission for all exiters in year 2000*/
bysort year: egen indc_n2=max(bar_e_ex)
replace bar_e_ex=indc_n2       /*input this value into each observation in 2000*/
drop indc_n2 indc_n


*--------------------------------------------------
*---------------------------------------------------------------------------------
*5-1(6)-2: Group level average: the group classification is by products: 
*        compute n_con: number of continuing products within continuing firms
*        compute n_en: number of new products within continuing firms
*        compute n_ex: number of exit products within continuing firms
*        compute bar_gamma_con1: average elasticity of all continuing firm-product pair in 2000;
*        compute bar_gamma_con2: average elasticity of all continuing firm-product pair in 2005;
*        compute bar_gamma_en: average elasticity of new products within continuing firms in 2005;
*        compute bar_gamma_ex: average elasticity of all exit products in year 2000;
*        compute bar_c_con1: average cost of contiuing firm-product pair in 2000
*        compute bar_c_con2: average cost of contiuing firm-product pair in 2005
*        compute bar_c_en: average cost of new products within continuing firms in 2000
*        compute bar_c_ex: average cost of exit products within continuing firms in 2005
*-------------------------------------------------------------------------------------
gen indc_n1=1 if quantity!=.&quantity_l!=.&status==1 /*indc_n1=1 for continuing firm-product pair*/
gen indc_n2=1 if quantity!=.&quantity_l==.&status==1&year==2 /*indc_n2=1 for new products within continuing firms*/
gen indc_n3=1 if quantity==.&quantity_l!=.&status==1 /*indc_n3=1 for exited products within continuing firms*/
bysort party_id: egen n_con=sum(indc_n1) if status==1 /*number of continuing firm-product pair within each continuing firm i*/
bysort party_id: egen n_en=sum(indc_n2) if status==1 /*number of new products within continuing firms*/
bysort party_id: egen n_ex=sum(indc_n3) if status==1 /*number of exit products withing continuing firms*/



bysort party_id product_code: egen indc_n=max(indc_n1)
replace indc_n1=indc_n
drop indc_n

bysort party_id product_code: egen indc_n=max(indc_n3)
replace indc_n3=indc_n
replace indc_n3=. if year==2


bysort party_id: egen bar_gamma_con1=sum(share_rho*theta2_E_fjt_x*indc_n1) if year==1
replace  bar_gamma_con1= bar_gamma_con1/n_con  /*average elasticity of all continuing firm-product pair in 2000;*/

bysort party_id: egen bar_c_con1=sum(quantity*MC_fp*indc_n1) if year==1
replace  bar_c_con1= bar_c_con1/n_con  /*average costs of all continuing firm-product pair in 2000;*/

*********************************************************************

bysort party_id: egen bar_gamma_con2=sum(share_rho*theta2_E_fjt_x*indc_n1) if year==2
replace  bar_gamma_con2= bar_gamma_con2/n_con  /*average elasticity of all continuing firm-product pair in 2005;*/

bysort party_id: egen bar_c_con2=sum(quantity*MC_fp*indc_n1) if year==2
replace  bar_c_con2= bar_c_con2/n_con  /*average costs of all continuing firm-product pair in 2005;*/
*************************************************************************


bysort party_id: egen bar_gamma_en=sum(share_rho*theta2_E_fjt_x*indc_n2) if year==2
replace  bar_gamma_en= bar_gamma_en/n_en  /*average elasticity of new products introduced by continuing firms in 2005;*/

bysort party_id: egen bar_c_en=sum(quantity*MC_fp*indc_n2) if year==2
replace  bar_c_en= bar_c_en/n_en  /*average costs of new products introduced by continuing firms in 2005;*/

**************************************************************************

bysort party_id: egen bar_gamma_ex=sum(share_rho*theta2_E_fjt_x*indc_n3) if year==1
replace  bar_gamma_ex= bar_gamma_ex/n_ex  /*average elasticity of exited products of continuing firms in 2000;*/
bysort party_id: egen bar_c_ex=sum(quantity*MC_fp*indc_n3) if year==1
replace  bar_c_ex= bar_c_ex/n_ex  /*average costs of new products introduced by continuing firms in 2005;*/

**************************************************************
* fill the value into each cell of continuing firm-product pair
bysort party_id product_code: egen bar_1=max(bar_gamma_con1)
bysort party_id product_code: egen bar_2=max(bar_gamma_con2)
bysort party_id product_code: egen bar_3=max(bar_gamma_en)
bysort party_id product_code: egen bar_4=max(bar_gamma_ex)
bysort party_id product_code: egen bar_5=max(bar_c_con1)
bysort party_id product_code: egen bar_6=max(bar_c_con2)
bysort party_id product_code: egen bar_7=max(bar_c_en)
bysort party_id product_code: egen bar_8=max(bar_c_ex)
replace bar_gamma_con1=bar_1
replace bar_gamma_con2=bar_2
replace bar_gamma_en=bar_3
replace bar_gamma_ex=bar_4
replace bar_c_con1=bar_5
replace bar_c_con2=bar_6
replace bar_c_en=bar_7
replace bar_c_ex=bar_8

drop bar_1 bar_2 bar_3 bar_4 bar_5 bar_6 bar_7 bar_8

drop indc_n indc_n1 indc_n2 indc_n3







/*
*-----------------------
egen bar_gamma_con1=sum(theta2_E_fjt_firm*indc_n) if year==1&status==1
replace bar_gamma_con1=bar_gamma_con1/N_con  /*average elasticity for all continuing firms in year 2000*/
bysort year: egen indc_n2=max(bar_gamma_con1)
replace bar_gamma_con1=indc_n2       /*imput this value into each observation in 2000*/
drop indc_n2


egen bar_gamma_con2=sum(theta2_E_fjt_firm*indc_n) if year==2&status==1
replace bar_gamma_con2=bar_gamma_con1/N_con  /*average elasticity for all continuing firms in year 2000*/
bysort year: egen indc_n2=max(bar_gamma_con2)
replace bar_gamma_con2=indc_n2       /*imput this value into each observation in 2000*/
drop indc_n2


egen bar_gamma_en=sum(theta2_E_fjt_firm*indc_n) if year==2&status==3
replace bar_gamma_en=bar_gamma_en/N_en  /*average elasticity for all entrants in year 2005*/
bysort year: egen indc_n2=max(bar_gamma_en)
replace bar_gamma_en=indc_n2       /*imput this value into each observation in 2005*/
drop indc_n2

egen bar_gamma_ex=sum(theta2_E_fjt_firm*indc_n) if year==1&status==2
replace bar_gamma_ex=bar_gamma_ex/N_ex  /*average elasticity for all exiters in year 2000*/
bysort year: egen indc_n2=max(bar_gamma_ex)
replace bar_gamma_ex=indc_n2       /*imput this value into each observation in 2000*/
drop indc_n2
*/



***************************************************************************************

*-------------------------------------------------------------------------
*5-1(7) compute delta_rho: the change of firm-product share
*-------------------------------------------------------------------------
gen diff_rho=share_rho-share_rho_l if status==1  /*for continuing firm-product pair share change*/

***************************************************************************************

*-------------------------------------------------------------------------
*5-1(8) compute delta_tau: the change of firm-level emission policy
*-------------------------------------------------------------------------


gen tau1=1/tax_emi if year==1& status==1 
bysort party_id: egen tau2=max(tau1) /*for each firm tau2 is the emission policy faced in period 1*/
gen delta_tau=1/tax_emi-tau2 
replace delta_tau=. if status==1&year==1 
bysort party_id: egen delta_tau1=mode(delta_tau)
replace delta_tau=delta_tau1
drop tau1 tau2 delta_tau1

***************************************************************************************

*-------------------------------------------------------------------------
*5-1(9) compute eta_e and ent_x: the number of entered and exit products
*-------------------------------------------------------------------------
gen prod_en=.
gen prod_ex=.
replace prod_en=1 if quantity!=.&quantity_l==.&year==2&status==1
replace prod_ex=1 if quantity==.&(quantity_l!=.)&status==1
bysort party_id: egen eta_e=sum(prod_en)
bysort party_id: egen eta_x=sum(prod_ex)
***************************************************************************************
/*
*We have the following constructed variable at hand:
(1). "bar_emission": average so2 emission for continuing firms in 2000 and 2005;
(2). "N_con", "N_en", "N_ex", and "N_total": number of continuing, entrant, exit and average number of firms.
(3). "bar_tau": the average of 1/tax for each continuing firm in 2000 and 2005;
(4). "bar_gamma": the average of elasticity to emission for each continuing firm in 2000 and 2005;
(5). "bar_c": the average total costs for each firm in 2000 and 2005;
(6). "bar_gamma_c": the avarege gamma*c_i for each firm in 2000 and 2005;
(7). "bar_rho": the average share of continuing firm-product pair in 2000 and 2005
(8). "bar_x": bar_x{ij}, the average quantity of x_ij at firm-product pair during 2000 - 2005;
(9). "bar_gamma_fp" the average elasticity of emission at firm-product level during 2000-2005;
(10). "bar_e_con1", "bar_e_con2": average emission for continuing group in 2000 and 2000, respectively;
(11). "bar_e_en" and "bar_e_ex": average emission for new entrants in year 2005 and for exiters in 2000, respectively.
(12). "bar_gamma_con1", "bar_gamma_con2": average elasticity for continuing firm-product pairs in 2000 and 2000, respectively;
(13). "bar_gamma_en" and "bar_gamma_ex": average elasticity for new  and for exit products within continuing firms, respectively.
(14). "bar_c_con1", "bar_c_con2": average costs for continuing firm-product pairs in 2000 and 2000, respectively;
(15). "bar_c_en" and "bar_c_ex": average costs for new and for exit products within continuing firms, respectively.
(14). "diff_quant": delta_x(ij) the quantity difference for continuing firm-product pair ij.
(16). "diff_quant_s": delta_x(j) the average quantity difference for all continuing firms.
(17). "diff_mc": the difference of firm-product level marginal cost between 2000 and 2005.
(18). "diff_rho": the difference of firm-product level share between 2000 and 2005.
(19). "diff_gamma": the difference of firm-product level emission elasticity between 2000 and 2005;
(20). "eta_e" and "eta_x": the number of entering and exit products for each firm. 
(21). "mc_con": average costs at firm-product level in 2000 and 2005; 
(22). "delta_tau": firm-level emission policy change during 2000-2005: tau_2005-tau_2000;
*/
***************************************************************************************
***************************************************************************************

*--------------------------------------------------------
* Step 2: Construct S_t (scale)
*-------------------------------------------------------- 

gen comp1=bar_emission*(N_en-N_ex) /*No of firms*/


gen comp21=bar_tau*bar_c*bar_gamma
egen max_comp21=max(comp21)
replace comp21=. if comp21==max_comp21


egen comp2=sum(comp21*(eta_e-eta_x)) 
replace comp2=N_total/N_con*comp2 /*adjusted number of products*/

gen aa=mc_con*diff_quant_s
egen min_aa=min(aa)
replace aa=. if aa==min_aa
bysort party_id: egen comp3= sum(aa) 
bysort party_id: egen comp31=max(comp3)
egen comp32=sum(bar_tau*bar_gamma*comp3)
replace comp3=N_total/N_con*comp32 /*adjusted average output growth*/
drop comp31 comp32

*Generate S_t
gen St=comp1+comp2+comp3
egen s1=mean(comp1)
egen s2=mean(comp2)
egen s3=mean(comp3)

drop comp1 comp2 comp3 comp21
***********************************************************************

*------------------------------------------------------------b
* Step 3: Construct P_t (emission policy)
*------------------------------------------------------------

gen gamma_tau=bar_gamma_c*delta_tau
egen ub_gamma_tau=pctile(gamma_tau),p(99)
egen Pt=sum(gamma_tau) if status==1&gamma_tau<ub_gamma_tau
replace Pt=N_total/N_con*Pt   



*****************************************************************************

*------------------------------------------------------------
* Step 4: Construct T_t (Technology)
*------------------------------------------------------------

bysort party_id: egen comp1=sum(bar_rho*diff_gamma)
egen comp12=sum(bar_tau*bar_c*comp1) if status==1
replace comp1=N_total/N_con*comp12 /*EBTC component*/

bysort party_id: egen comp2=sum(bar_x*diff_mc)
egen comp22=sum(bar_tau*bar_gamma*comp2)
replace comp2=N_total/N_con*comp22  /*Eifficiency component*/

gen Tt=comp1+comp2

egen t1=mean(comp1)
egen t2=mean(comp2)

drop comp1 comp12 comp2 comp22



****************************************************************************************



*------------------------------------------------------------
* Step 5: Construct R_t (Reallocation)
*------------------------------------------------------------
bysort party_id: egen comp1=sum(mc_con*(diff_quant_s-diff_quant))
egen max_comp1=max(comp1)
egen min_comp1=min(comp1)


replace comp1=. if comp1==max_comp1
replace comp1=. if comp1==min_comp1

gen compw=bar_tau*bar_gamma*comp1
sum compw,d

egen ub_compw=pctile(compw),p(99.9)
egen lb_compw=pctile(compw),p(3)
replace compw=. if compw>=ub_compw|compw<=lb_compw
egen comp12=sum(compw) if status==1
replace comp1=N_total/N_con*comp12 /*Across firms component*/
sum comp1

bysort party_id: egen comp2=sum(bar_gamma_fp*diff_rho) if status==1
gen  comp22=bar_tau*bar_c*comp2 if status==1

egen min_comp22=pctile(comp22),p(0.5)
replace comp22=. if comp22<=min_comp22

egen comp23=sum(comp22)
replace comp2=N_total/N_con*comp23 /*Across Products component*/

gen Rt=comp1+comp2
sum comp1 comp2
egen r1=mean(comp1)
egen r2=mean(comp2)
drop comp1 comp12 comp2 comp22 comp23

 
*****************************************************************


*------------------------------------------------------------
* Step 6: Construct C_t (Churning)
*------------------------------------------------------------

gen comp11=N_en*(bar_e_en-bar_e_con2)
egen comp11_new=mode(comp11)
replace comp11=comp11_new
drop comp11_new
gen comp12=N_ex*(bar_e_con1-bar_e_ex)
egen comp12_new=mode(comp12)
replace comp12=comp12_new
drop comp12_new
gen comp1=comp11+comp12 /*Firm-level Churning*/


egen comp2=sum(bar_tau*bar_c*(n_en*(bar_gamma_en-bar_gamma_con2)+n_ex*(bar_gamma_con1-bar_gamma_ex))) if status==1
replace comp2=N_total/N_con*comp2   /*Product-level emissions churning*/


egen comp3=sum(bar_tau*bar_gamma*(n_en*(bar_c_en-bar_c_con2)+n_ex*(bar_c_con1-bar_c_ex))) if status==1
replace comp3=N_total/N_con*comp3 /*Product-level cost churning*/


gen Ct=comp1+comp2+comp3
egen c1=mean(comp1)
egen c2=mean(comp2)
egen c3=mean(comp3)
drop comp1 comp2 comp3



********************************************





******************************************Table H5: multi-tech*****************************************************************

******************************************Table H5: multi_tech*****************************************************************


******************************************Table H5: multi_tech*****************************************************************



*---------------------------------------------------------
* Row 1 (columns 1-3) of Table H5 (for multi-Technology)
*-----------------------------------------------------------
gen Delta_E=St+Pt+Tt+Rt+Ct

gen Scale=St/Delta_E*100
gen Policy=Pt/Delta_E*100
gen Technology=Tt/Delta_E*100
gen Reallocation=Rt/Delta_E*100
gen Churning=Ct/Delta_E*100


rename Reallocation Composition 
replace Composition=Composition+Churning 
replace Technology=Technology+Policy 

sum Scale Composition Technology Churning

********************Table H6 for multi-tech*************************
********************Table H6 for multi-tech*************************
*---------------------------------------------------------------
* The following code replicate the subcomponent share in Table H6
*-----------------------------------------------------------------
* (1) Subshare of Scale (row 1 and columns 2-4 of Table H6)
gen share_s1=s1/St*100
gen share_s2=s2/St*100
gen share_s3=s3/St*100

sum share_s1 share_s2 share_s3

**********************************************
* (2)subshare of Composition (row3 and columns 2-4  of Table H6)
gen Rt_new=Rt+Ct
gen share_r1=r1/Rt*100*(Rt/Rt_new)
gen share_r2=r2/Rt*100*(Rt/Rt_new)
gen share_r3=Ct/Rt*100*(Rt/Rt_new)

sum share_r1 share_r2 share_r3
*********************************************
* (3) subshare of Technology (row 5 and columns 2-4 of Table H6)

gen Tt_new=Tt+Pt
gen share_t1=t1/Tt*100*(Tt/Tt_new)
gen share_t2=t2/Tt*100*(Tt/Tt_new)
gen share_t3=Pt/Tt*100*(Tt/Tt_new)

sum share_t1 share_t2 share_t3




********************Table H7 for multi-tech*************************
********************Table H7 for multi-tech*************************
* (4)subshare of Churning
gen share_c1=c1/Ct*100
gen share_c2=c2/Ct*100
gen share_c3=c3/Ct*100


sum share_c1 share_c2 share_c3

**********************************************************************************************



